﻿#include "loading_widget.h"
#include <QDebug>
#include <QPainter>


LoadingAnimationWidget::LoadingAnimationWidget(QWidget *parent) :
    QWidget(parent),
    m_rotate(false),
    m_timer(nullptr),
    m_pixmap(nullptr)
{
    m_timer = new QTimer(this);
    qDebug()<<"LoadingAnimationWidget";
    connect(m_timer,SIGNAL(timeout()),this,SLOT(timerout()));
}

void LoadingAnimationWidget::SetPixmap(QPixmap *p)
{
    connect(m_timer,SIGNAL(timeout()),this,SLOT(timerout()));

    m_timer->setInterval(100);
    m_timer->start(40);
    m_pixmap = p;
}

void LoadingAnimationWidget::timerout()
{
    m_rotate =true;
    update();
}

void LoadingAnimationWidget::paintEvent(QPaintEvent *)
{
    static int rotate = 0;
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,true);
    painter.translate(width()/2,height()/2);

    if(m_rotate)
    {
       rotate = (rotate+5)%360;
       m_rotate =false;
    }

    painter.rotate(rotate);
    if(nullptr != m_pixmap)
        painter.drawPixmap(-m_pixmap->width()/2,-m_pixmap->height()/2,*m_pixmap);
}
